package com.shinbada.modules.competitor.web;

import com.shinbada.excel.modle.ImportExcelResult;
import com.shinbada.excel.modle.UploadModle;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import java.util.List;
import java.util.ArrayList;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.ConstraintViolationException;

import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import com.google.common.collect.Lists;
import com.shinbada.common.utils.DateUtils;
import com.shinbada.common.json.AjaxJson;
import com.shinbada.core.persistence.Page;
import com.shinbada.core.web.BaseController;
import com.shinbada.common.utils.StringUtils;
import com.shinbada.modules.competitor.entity.CompetitorProductAttribute;
import com.shinbada.modules.competitor.service.CompetitorProductAttributeService;

import com.shinbada.excel.ExcelKit;

/**
 * 产品属性Controller
 * @author initcap
 * @version 2021-08-04
 */
@RestController
@RequestMapping(value = "/competitor/competitorProductAttribute")
@Api(tags = "产品属性")
public class CompetitorProductAttributeController extends BaseController {

	@Autowired
	private CompetitorProductAttributeService competitorProductAttributeService;

	@ModelAttribute
	public CompetitorProductAttribute get(@RequestParam(required=false) String id) {
		CompetitorProductAttribute entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = competitorProductAttributeService.get(id);
		}
		if (entity == null){
			entity = new CompetitorProductAttribute();
		}
		return entity;
	}

	@RequiresPermissions("user")
	@GetMapping("list")
	@ApiOperation("产品属性列表数据")
	public AjaxJson list(CompetitorProductAttribute competitorProductAttribute, HttpServletRequest request, HttpServletResponse response) {
		Page<CompetitorProductAttribute> page = competitorProductAttributeService.findPage(new Page<>(request, response), competitorProductAttribute);
		return AjaxJson.success().put("page",page);
	}

	@RequiresPermissions("user")
	@GetMapping("queryById")
	@ApiOperation("根据Id获取产品属性数据")
	public AjaxJson queryById(CompetitorProductAttribute competitorProductAttribute) {
		return AjaxJson.success().put("competitorProductAttribute", competitorProductAttribute);
	}

	@RequiresPermissions(value={"competitor:competitorProductAttribute:add","competitor:competitorProductAttribute:edit"},logical=Logical.OR)
	@PostMapping("save")
	@ApiOperation("保存产品属性")
	public AjaxJson save(CompetitorProductAttribute competitorProductAttribute) {
		/**
		 * 后台hibernate-validation插件校验
		 */
		String errMsg = beanValidator(competitorProductAttribute);
		if (StringUtils.isNotBlank(errMsg)){
			return AjaxJson.error(errMsg);
		}
		//新增或编辑表单保存
		competitorProductAttributeService.save(competitorProductAttribute);
		return AjaxJson.success("保存产品属性成功");
	}

	@RequiresPermissions("competitor:competitorProductAttribute:del")
	@DeleteMapping("delete")
	@ApiOperation("批量删除产品属性")
	public AjaxJson delete(String ids) {
		String[] idArray =ids.split(",");
		for(String id : idArray){
			competitorProductAttributeService.delete(competitorProductAttributeService.get(id));
		}
		return AjaxJson.success("删除产品属性成功");
	}

	@RequiresPermissions("competitor:competitorProductAttribute:export")
	@GetMapping("export")
	@ApiOperation("导出产品属性")
	public void export(CompetitorProductAttribute entity, HttpServletResponse response) {
		ExcelKit.$Export(CompetitorProductAttribute.class, response).downXlsx(competitorProductAttributeService.findList(entity), false);
	}

	@RequiresPermissions("competitor:competitorProductAttribute:import")
	@GetMapping("template")
	@ApiOperation("导出产品属性模版")
	public void export(HttpServletResponse response) {
		ExcelKit.$Export(CompetitorProductAttribute.class, response).downXlsx(new ArrayList<>(), true);
	}
	
	@RequiresPermissions("competitor:competitorProductAttribute:import")
	@PostMapping(value = "import")
	@ApiImplicitParam(name = "modle", paramType = "query", allowableValues = "skip,exit,cover")
	@ApiOperation("导入文件,下载模版为/export")
	public AjaxJson uploadExcel(MultipartFile file, UploadModle modle) {
		if (shinProperites.isDemoMode()) {
			return AjaxJson.error("演示模式，不允许操作！");
		}
		ImportExcelResult<CompetitorProductAttribute> result = competitorProductAttributeService.uploadExcel(file, modle);
		return AjaxJson.success(result);
	}

}